//boolean status for learning
var learning = false;

//lesson contents
var lesson = [
'第一课：声母键位练习(空格继续)', 
'大家好！这是双拼输入法的第一课。我们将从汉字的声母输入开始。', 
'我们都知道汉字的输入分为声母韵母部，但是在输入声母的时候，实际上好多键是永远摁不到的哟', 
'那么双拼输入法就是利用分别用闲置的声母、韵母键位互相取代来提高你的输入效率。', 
'实施上除去大多数按一只键即可声母之外，只要记住三个新的韵母按键，就可以掌握双拼输入的韵母部分咯！', 
'相信你已经猜到这三个声母分别是zh、ch、sh，在小鹤双拼的方案中，它们分别对应于键盘上的v、i、u三个键，下方的键盘中红色突出显示，你也可以低头看看自己的键盘，分别离你的左手食指、右手中指和右手食指很近哟～', 
'好那下面我们就开始一起记住它们吧，接下来的时间里你要在看到"zh"个声母的同时，快速的敲击"v"键!', 
'zh',
'zh',
'zh',
'很好，看来你已经学会了输入"zh"，下面是"ch"，对应的按键是"i",', 
'ch',
'ch', 
'ch',
'太棒了，接下来轮到"sh"了，记得敲"u"键',
'sh',
'sh',
'sh',
'恩，很不错，让我们来复习一下：', 
'zh',
'ch',
'sh',
'zh',
'第一课的内容就是这样咯，回见！^^'
];

//current position of lesson
var curPos = 0;

var goalKey;

var showNext = function(){
	if (!learning) return;
	$('#text').fadeOut("slow", function(){
		if (lesson[curPos] == "zh"){
			goalKey = "v";
		}
		else if (lesson[curPos] == "ch"){
			goalKey = "i";
		}
		else if (lesson[curPos] == "sh"){
			goalKey = "u";
		}
		else goalKey = " ";
		document.getElementById("text").innerHTML = lesson[curPos];
		$('#text').removeClass("accepted");
		$('#text').fadeIn("fast");
	});
}
//accept the input for the current position.
var accept = function(pos){
	document.getElementById("text").className = "accepted";

	if (curPos >= lesson.length-1){
		learning = false;
		alert("Conguatulations! You have finish this course!!");
		document.getElementById("text").innerHTML = "";
	}
}

$(document).ready(function(){
	learning = true;
	curPos = 0;
	showNext();
    	$("body").keydown(function(e){
		if (e.which == 27){
			$(location).attr('href', 'dashboard.php');
		}	
	});
  	$("body").keypress(function(e){
		var e = e || event;
		var keyPressed = e.keyCode || e.which;
		if (!learning) return;
		console.log("pressed key number " + keyPressed);
		var test=String.fromCharCode(keyPressed);
		//alert(test);
		if(test!=' ')
		{
			$("div button[data-key-value="+test+"]").trigger("focus");
		}
		else
			$("div button[data-key-value=' ']").trigger("focus");
		if (keyPressed == goalKey.charCodeAt()){
			//type accepted!
			console.log("accepted pressed character : " + keyPressed);
			accept(curPos);
			curPos ++;
			showNext();
		}
		else{
			//type wrong char!
			console.log("wrong pressed character : " + keyPressed + " !   should be :" + goalKey + ", CHAR CODE : " + goalKey.charCodeAt());
		}

	});

	$("div button").click(function(){
		var input=$(this).attr("data-key-value");
		if(input!=' ')
		{
			$("div button[data-key-value="+input+"]").trigger("focus");
		}
		else
			$("div button[data-key-value=' ']").trigger("focus");

		if (!learning) return;
		console.log("cliked key character:" + input);	
		if (input.charCodeAt() == goalKey.charCodeAt()){
			//type accepted!
			console.log("accepted clicked character : " + input);
			accept(curPos);
			curPos ++;
			showNext();
		}
		else{
			//type wrong char!
			console.log("wrong clicked character : " + input + " !   should be :" + goalKey + ", CHAR CODE : " + goalKey.charCodeAt());
		}
	});
});
